package net.framework.smc.demo.controller;

import lombok.extern.slf4j.Slf4j;
import net.framework.smc.demo.po.ResultPo;
import net.framework.smc.demo.po.Trip;
import net.framework.smc.demo.service.TripService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * @authore zyx
 * @date 2018/6/28 15:29
 */
@RestController
@Slf4j
@RequestMapping("/trip")
public class TripController {

    @Autowired
    TripService tripService;

    @RequestMapping(value = "/list/{type}", method = RequestMethod.GET)
    public ResultPo listTrip(@PathVariable("type") String type,   @ModelAttribute("userId") String userId){
        try {
            return tripService.listTrip(type, userId);
        }catch (Exception e) {
            log.error(e.toString());
            return ResultPo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultPo delTrip(@PathVariable("id") String id) {
        try {
            return tripService.delTrip(id);
        }catch (Exception e) {
            log.error(e.toString());
            return ResultPo.error(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResultPo addTrip(@RequestBody Trip trip) {
        try {
            return tripService.addTrip(trip);
        }catch (Exception e) {
            log.error(e.toString());
            return ResultPo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/finished/{id}",method = RequestMethod.PUT)
    public ResultPo endTrip(@PathVariable String id) {
        try {
            return tripService.finishedTrip(id);
        }catch (Exception e) {
            log.error(e.toString());
            return ResultPo.error(e.getMessage());
        }
    }
}
